package com.spring.sunflower.userinfo;

import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.common.CommonContract;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.util.NameLengthFilter;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Kevin on 2021/6/30<br/>
 * <p>
 * Describe:<br/>
 */
public class NicknameEditActivity extends BaseActivity<NickNameEditPresenter> implements CommonContract.NicknameEditView {
    private EditText etNickname;
    private ImageView ivClear;
    // 禁止EditText输入空格
    private InputFilter filter = (source, start, end, dest, dstart, dend) -> {
        if (source.equals(" ")) return "";
        else return null;
    };

    @Override
    public void initView() {
        Intent intent = getIntent();
        String nickName = intent.getStringExtra(Constant.USER_NICKNAME);
        tvTitle.setText("编辑昵称");
        tvFunctionX.setText("保存");
        etNickname = findViewById(R.id.etNickname);
        etNickname.setText(nickName);
        etNickname.setSelection(etNickname.length());
        etNickname.setFilters(new InputFilter[]{filter});
        ivClear = findViewById(R.id.ivClear);
//        ivClear.setVisibility(View.INVISIBLE);
        InputFilter[] filter = {new NameLengthFilter(16, this)};
        etNickname.setFilters(filter);
        etNickname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (etNickname.getText().toString().trim().length() == 0) {
                    ivClear.setVisibility(View.INVISIBLE);
                } else {
                    ivClear.setVisibility(View.VISIBLE);
                }
            }
        });
        ivClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etNickname.setText("");
            }
        });
        tvFunctionX.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nickname = etNickname.getText().toString().trim();
                if (TextUtils.isEmpty(nickname)) {
                    showToast("昵称不能为空");
                } else {
                    Map<String, String> map = new HashMap<>();
                    map.put("nickname", nickname);
                    mPresenter.updateUserInfo(map);
                }
            }
        });

    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_nickname_edit;
    }

    @Override
    public NickNameEditPresenter setPresenter() {
        return new NickNameEditPresenter(this);
    }

    @Override
    public void updateSuccess() {
        Intent intent = new Intent(this, UserInfoEditActivity.class);
        intent.putExtra(Constant.USER_NICKNAME, etNickname.getText().toString().trim());
        setResult(RESULT_OK, intent);
        showToast("保存成功");
        finish();

    }

    @Override
    public void updateFailed() {
        showToast("保存失败:(");
    }
}
